package authhandler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"time"
	"yundian/internel/app/dao"
	"yundian/internel/app/helper"
	"yundian/internel/app/helper/request"
	p "yundian/internel/app/models/permissions"
	"yundian/internel/pkg/dingo"
	. "yundian/internel/pkg/dingo/result"
)

// ------- 权限相关API请求处理 permissionHandler -------

// GetActionListHandler 获取所有操作权限组 (新增、查询、详情、修改、删除、导入、导出、启用、禁用)
func GetActionListHandler(ctx *gin.Context)  {
	Response(ctx)("获取成功", CodeSuccess, NewActionList())(OK)
}

// GetPermissionList 查询 - 有分页、有查询参数
func HandleGetPermissionList(ctx *gin.Context)  {
	// 请求参数处理
	param := &request.PermissionListReq{}
	dingo.ErrorIf(ctx.ShouldBindQuery(param)).Unwrap()

	data := dao.NewPermissionDao().GetPermissionList(param)

	// 响应请求
	Response(ctx)("获取成功",CodeSuccess,
		ResponseList{
			PageNo:     param.PageNo,
			PageSize:   param.PageSize,
			Data:       data.Unwrap(),
			TotalCount: data.Val().(int64), // 总条数
			TotalPage:  0, // 总分页数
		},
	)(OK)
}

// GetPermissionListNoPager 查询 - 无分页
func HandleGetPermissionListNoPager(ctx *gin.Context)  {
	permissions := dao.NewPermissionDao().GetPermissionListNoPager().Unwrap().([]*p.PermissionsModel)
	// 响应请求
	Response(ctx)("获取成功", CodeSuccess, permissions)(OK)
}

// GetPermissionListRoleId 查询 - RoleID
func HandleGetPermissionListRoleId(ctx *gin.Context)  {
	//roleId := &struct {
	//	Id int `form:"id"`
	//}{}
	//if err := ctx.ShouldBindQuery(roleId); err != nil {
	//	if e, ok := err.(validator.ValidationErrors); ok {
	//		Response(ctx)("", CodeInvalidParam, e.Translate(global.Trans))(ParamError)
	//		return
	//	}
	//}
	//if roleId.Id == 0 {
	//	permissions := getter.RouteGetter.Gets().Unwrap().([]*routes.RoutesModel)
	//	permissions = actionsHandle(permissions)
	//	// 响应请求
	//	Response(ctx)("", CodeSuccess, permissions)(OK)
	//} else {
	//	permissions := getter.RouteGetter.GetFromRoleId(roleId.Id).Unwrap().([]*routes.RoutesModel)
	//	permissions = actionsHandle(permissions)
	//	// 响应请求
	//	Response(ctx)("", CodeSuccess, permissions)(OK)
	//}
}

// GetPermission 查询 - 详情
func HandleGetPermission(ctx *gin.Context)  {
	//routeId := &struct {
	//	Id int `uri:"id" binding:"required,gt=0"`
	//}{}
	//if err := ctx.ShouldBindUri(routeId); err != nil {
	//	Response(ctx)("GetAdminRouteById with invalid param", CodeInvalidParam,err)(Error)
	//}
	//info := getter.RouteGetter.GetFromId(routeId.Id).Unwrap().(*routes.RoutesModel)
	//actions := make([]string, 0)
	//_ = json.Unmarshal([]byte(info.Actions), &actions)
	//actionEntitySet := generateActionData(actions)
	//b,_ := json.Marshal(actionEntitySet)
	//info.ActionData = string(b)
	//Response(ctx)("", CodeSuccess, info)(OK)
}

// InsertPermission 新增
func HandleInsertPermission(ctx *gin.Context)  {
	m := p.New().Mutate(p.WithCreateAt(dingo.Time(time.Now().Unix())))
	dingo.ErrorIf(ctx.ShouldBindJSON(m)).Unwrap()
	// 空值时- []
	if m.Actions == "" {
		action,_ := json.Marshal([]string{})
		m.Actions = string(action)
		m.ActionData = string(action)
	}
	//log.Println(m)
	// 生成actionData
	if actData := helper.GenerateActionData(m.Actions); actData != "" {
		m.ActionData = actData
	}
	Response(ctx)("新增成功", CodeSuccess, dao.NewPermissionDao().InsertPermission(m))(OK)
}

// UpdatePermission 修改
func HandleUpdatePermission(ctx *gin.Context)  {
	m := p.New()
	dingo.ErrorIf(ctx.ShouldBindJSON(m)).Unwrap()
	m.UpdateAt = dingo.Time(time.Now().Unix())
	// 空值时- []
	if m.Actions == "" {
		action,_ := json.Marshal([]string{})
		m.Actions = string(action)
		m.ActionData = string(action)
	}
	//log.Println(m)
	// 生成actionData
	if actData := helper.GenerateActionData(m.Actions); actData != "" {
		m.ActionData = actData
	}
	Response(ctx)("更新成功", CodeSuccess, dao.NewPermissionDao().UpdatePermission(m).Unwrap())(OK)
}

// DeletePermission 删除
func HandleDeletePermission(ctx *gin.Context)  {
	param := &struct {
		PermissionId int `json:"id" binding:"required"`
	}{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	Response(ctx)("删除成功", CodeSuccess, dao.NewPermissionDao().DeletePermission(param.PermissionId).Unwrap())(OK)
}

